<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-03-14 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{	
	function __construct($request)
	{
		parent::__construct($request);

		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;
		
		$this->request['bid'] = isset($this->request['bid']) ? (int)$this->request['bid'] : '';
		$this->request['bname'] = isset($this->request['bname']) ? trim($this->request['bname']) : '';
		$this->request['content'] = isset($this->request['content']) ? trim($this->request['content']) : '';
		
		$bid = $this->request['bid'];
		$bname = $this->request['bname'];
		$content = $this->request['content'];
		
		$query_link = "manage.php?m=customer_comment";
		$query_link .= $bid ? '&business_id='.$this->request['bid'] : '';
		$query_link .= $bname ? '&business_name='.$this->request['bname'] : '';
		$query_link .= $content ? '&content='.$this->request['content'] : '';
				
		$result = search_comment_list($this->request);
		
		$total_record = $result['count'];
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '10 20 50 100 200 500');
		
		$list = $result['result'];
		include template('admin/business_comment');
	}
	
	private function modify()
	{		
		$id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=customer_comment');
		}
		
		$info = get_business_comment(array('id'=>$id));
		
		if(!isset($info['id']) || $info['id'] < 1)
		{
			$this->messager("评论不存在", 'manage.php?m=customer_comment');
		}

		$action_name = '编辑评论';
		include template('admin/business_comment_info');
	}
	
	private function doModify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['id']) ? $this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'manage.php?m=customer_comment');
		}
		
		$comment = get_business_comment(array('id'=>$id));
		
		if(!isset($comment['id']) || $comment['id'] < 1)
		{
			$this->messager("评论不存在", 'manage.php?m=customer_comment');
		}
		
		$info['id'] = $id;
		$info['service_score'] = isset($this->request['service_score']) ? (float)$this->request['service_score'] : '';
		$info['env_score'] = isset($this->request['env_score']) ? (float)$this->request['env_score'] : '';
		$info['price_score'] = isset($this->request['price_score']) ? (float)$this->request['price_score'] : '';
		$info['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		$info['is_show'] = isset($this->request['is_show']) ? '1' : '0';
		
		if(strlen($info['desc']) > 2999)
		{
			//$this->messager("描述超长", 'manage.php?m=customer_comment&a=modify&id='.$id);
			cut_string($info['desc'], 2995);
		}
		
		$info['uid'] = $this->user['id'];
		
		$result = update_business_comment($info);
		
		if($result)
		{
			$this->messager("修改成功", 'manage.php?m=customer_comment');
		}
		else
		{
			$this->messager("修改失败", 'manage.php?m=customer_comment');
		}
	}
	
	private function doDelete()
	{		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
				
		$redirect = 'manage.php?m=customer_comment';
		
		if(!$ids)
		{
			$this->messager("请指定ID", $redirect);
		}
				
		$result = false;
		if(check_array($ids))
		{
			foreach($ids as $id)
			{
				$result = delete_business_comment(array('id'=>$id));
			}
		}
		else
		{
			$result = delete_business_comment(array('id'=>$ids));
		}
				
		if($result)
		{
			$this->messager("删除成功", $redirect);
		}
		else
		{
			$this->messager("删除失败", $redirect);
		}
	}
}